﻿using Microsoft.AspNetCore.Builder;
using Xc.DataAccess.WaterMeters.ClientSides.Services;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ApplicationExtension
    {
        /// <summary>
        /// 添加水表设备回调服务
        /// <list type="number">
        /// <item>水表回调服务 <see cref="IWaterDeviceReceiveService"/></item>
        /// <item>水表发送服务 <see cref="IWaterDeviceSendService"/></item>
        /// </list>
        /// </summary>
        /// <param name="serviceDescriptors"></param>
        /// <returns></returns>
        public static IServiceCollection AddWaterMeterDevice<TWaterMeterService>(this IServiceCollection serviceDescriptors) where TWaterMeterService :  class, IWaterDeviceReceiveService
        {
            serviceDescriptors.AddHttpClient();
            serviceDescriptors.AddSingleton<IWaterDeviceSendService, WaterDeviceSendService>();
            serviceDescriptors.AddTransient<IWaterDeviceReceiveService,TWaterMeterService>();
            return serviceDescriptors; 
        }
        /// <summary>
        /// 使用表具设备服务
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="serverPath"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseMeterDevice(this IApplicationBuilder applicationBuilder,string serverPath)
        {
           var service=  applicationBuilder.ApplicationServices.GetService<IWaterDeviceSendService>();
            service.ServerPath = serverPath;
            return applicationBuilder;
        }
    }
}
